﻿
//==============================================================================
// 文件     ：ByteHelper.cs
// 功能概要 ：常用的byte方法
//           
//           
// 作者    ：周薇
// 时间    ：2011年5月16日
// Copyright (c) 2011 重庆安运科技公司. All rights reserved.
//==============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Webcar.Utils
{
    /// <summary>
    /// 常用的byte方法
    /// </summary>
    public static class ByteHelper
    {
        /// <summary>
        /// byte[]转换为Base64字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToBase64String(this byte[] bytes)
        {
            return Convert.ToBase64String(bytes);
        }
        /// <summary>
        /// byte[]转换为字符串，编码为gb2312
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToDefaultString(this byte[] bytes)
        {
            return Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length).TrimEnd('\0');
        }
        /// <summary>
        /// byte[]按照gb2312编码转换为字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToDefaultStringTrim(this byte[] bytes)
        {
            return Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length).Trim().TrimEnd('\0').TrimStart('\0');
        }
        /// <summary>
        /// byte[]按照gb2312编码转换为字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string ToDefaultString(this byte[] bytes,int len)
        {
            return Encoding.GetEncoding("gb2312").GetString(bytes, 0, len).Trim();
        }
        /// <summary>
        /// 是否数组都是0
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static bool IsZeroBytes(this byte[] bytes)
        {
            foreach (byte b in bytes)
            {
                if (b != 0x00) return false;
            }
            return true;
        }
        /// <summary>
        /// BitConvert.ToString()的逆向函数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] HexString2Byte(string value)
        {
            if (value.IsNull())
                return new byte[0];
            if ((value.Length % 2) != 0)
                value = "0" + value;
            byte[] returnBytes = new byte[value.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                string s = value.Substring(i * 2, 2);
                returnBytes[i] = byte.Parse(s, System.Globalization.NumberStyles.HexNumber);
            }
            return returnBytes;
        }
        public static byte[] BCDString2Bytes(string value)
        {
            if (value.IsNull())
                return new byte[0];
            if ((value.Length % 2) != 0)
                value = "0" + value;
            byte[] returnBytes = new byte[value.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                string s = value.Substring(i * 2, 2);
                returnBytes[i] = byte.Parse(s);
            }
            return returnBytes;
        }

        public static int HexString2Byte(string value,byte[] returnBytes)
        {
            if (value.IsNull())
                return 0;
            if ((value.Length % 2) != 0)
                value = "0" + value;
            //byte[] returnBytes = new byte[value.Length / 2];
            for (int i = 0; i < value.Length/2; i++)
            {
                string s = value.Substring(i * 2, 2);
                //byte.Parse(
                returnBytes[i] = byte.Parse(s, System.Globalization.NumberStyles.HexNumber);
            }
            return value.Length / 2;
        }
        public static int ToUInt32NetOrder(this byte[] bytes, int start)
        {
            uint num = BitConverter.ToUInt32(bytes,start);
            return (int)Win32.htonl(num);
        }
        public static int ToUInt32NetOrder(this byte[] bytes)
        {
            return bytes.ToUInt32NetOrder(0);
        }

        public static int ToUInt32(this byte[] bytes, int start)
        {
            return BitConverter.ToInt32(bytes, start);
        }
        public static int ToUInt32(this byte[] bytes)
        {
            return bytes.ToUInt32(0);
        }
        public static ushort ToUShortNetOrder(this byte[] bytes, int start)
        {
            ushort ret = BitConverter.ToUInt16(bytes,start);
            return Win32.htons(ret);
        }
        public static ushort ToUShortNetOrder(this byte[] bytes)
        {
            return bytes.ToUShortNetOrder(0);
        }

        public static ushort ToUShort(this byte[] bytes, int start)
        {
            return BitConverter.ToUInt16(bytes, start);
        }
        public static ushort ToUShort(this byte[] bytes)
        {
            return bytes.ToUShort(0);
        }

        public static byte[] ToBytesNetOrderUInt32(this int str)
        {
            uint num = Win32.ntohl((uint)str);
            return BitConverter.GetBytes(num);
        }
        public static byte[] ToBytesNetOrderUShort(this int str)
        {
            ushort ret = Win32.ntohs((ushort)str);
            return BitConverter.GetBytes(ret);
        }

        public static byte[] ToBytesUInt32(this int str)
        {
            return BitConverter.GetBytes(str);
        }
        public static byte[] ToBytesUShort(this int str)
        {
            return BitConverter.GetBytes((ushort)str);
        }
    }
}
